package com.kgcx.schedule.service;

import com.kgcx.common.constant.Cons;
import com.kgcx.core.utils.HelpUtil;
import com.kgcx.core.utils.JsonUtil;
import com.kgcx.schedule.domain.ScheduleParameterDomain;
import com.kgcx.schedule.model.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;

@Service
// @Transactional(rollbackFor = Exception.class)
public class ScheduleSettingsService {
    // private static final Logger LOG = LoggerFactory.getLogger(ScheduleSettingsService.class);
    /**
     * 您的用车需求已派单成功。${driverName} ${autoNo} 电话${driverPhone}，请您按时到达指定上车地点。
     */
    private static final String DEFAULT_MSG_ID = "SMS_115760505";

    @Resource
    private ScheduleParameterService scheduleParameterService;

    @Transactional(rollbackFor = Exception.class)
    public void resetSettings(Long city_id) {
        updateSetting(city_id, buildDefaultOptionallyBook());
        updateSetting(city_id, buildDefaultOptionallyNow());
        updateSetting(city_id, buildDefaultSpeciallyBook());
        updateSetting(city_id, buildDefaultSpeciallyNow());
    }

    @Transactional(rollbackFor = Exception.class)
    public void updateSetting(Long city_id, ScheduleSetting setting) {
        setting.setUpdate_id(0L);
        setting.setUpdate_time(HelpUtil.getNowTime());

        ScheduleParameterDomain p = scheduleParameterService.selectParameter(setting.getClass().getName(), city_id);
        if (p != null) {
            p.setValue(JsonUtil.toString(setting));
            scheduleParameterService.updateEntity(p);
            return;
        }

        p = new ScheduleParameterDomain();
        p.setCode(setting.getClass().getName());
        p.setName(setting.getName());
        p.setOrder(1);
        p.setCity_id(city_id);
        p.setCreate_id(0L);
        p.setCreate_time(HelpUtil.getNowTime());
        p.setUpdate_id(0L);
        p.setUpdate_time(HelpUtil.getNowTime());

        p.setValue(JsonUtil.toString(setting));
        scheduleParameterService.insertEntity(p);
    }

    @SuppressWarnings("unchecked")
    public <T> T getSetting(Long city_id, Class<T> clazz) {
        T setting = scheduleParameterService.getParameterValue(city_id, clazz);
        if (setting != null) {
            return setting;
        }

        if (OptionallyBook.class == clazz) {
            return (T) buildDefaultOptionallyBook();
        }
        if (OptionallyNow.class == clazz) {
            return (T) buildDefaultOptionallyNow();
        }
        if (SpeciallyBook.class == clazz) {
            return (T) buildDefaultSpeciallyBook();
        }
        if (SpeciallyNow.class == clazz) {
            return (T) buildDefaultSpeciallyNow();
        }

        return null;
    }

    private OptionallyBook buildDefaultOptionallyBook() {
        OptionallyBook domain = new OptionallyBook();
        domain.setIs_send_msg(Cons.BOOLEAN_TRUE);
        domain.setMsg_id(DEFAULT_MSG_ID);
        domain.setNotify_time(120);
        domain.setMin_time(120);
        domain.setMax_time(3);
        domain.setRounds(new ArrayList<ScheduleRound>() {
            private static final long serialVersionUID = 1L;
            {
                add(new ScheduleRound(5000, 0));
            }
        });
        return domain;
    }

    private OptionallyNow buildDefaultOptionallyNow() {
        OptionallyNow domain = new OptionallyNow();
        domain.setIs_send_msg(Cons.BOOLEAN_TRUE);
        domain.setMsg_id(DEFAULT_MSG_ID);
        domain.setIs_standby(Cons.BOOLEAN_TRUE);
        domain.setIs_online(Cons.BOOLEAN_TRUE);
        domain.setRounds(new ArrayList<ScheduleRound>() {
            private static final long serialVersionUID = 1L;
            {
                add(new ScheduleRound(5000, 0));
            }
        });
        return domain;
    }

    private SpeciallyBook buildDefaultSpeciallyBook() {
        SpeciallyBook domain = new SpeciallyBook();
        domain.setIs_send_msg(Cons.BOOLEAN_TRUE);
        domain.setMsg_id(DEFAULT_MSG_ID);
        domain.setIs_standby(Cons.BOOLEAN_TRUE);
        domain.setIs_online(Cons.BOOLEAN_TRUE);
        domain.setNotify_time(30);
        domain.setMin_time(40);
        domain.setMax_time(3);
        domain.setNowRounds(new ArrayList<ScheduleRound>() {
            private static final long serialVersionUID = 1L;
            {
                add(new ScheduleRound(5000, 0));
            }
        });
        domain.setBookRounds(new ArrayList<ScheduleRound>() {
            private static final long serialVersionUID = 1L;
            {
                add(new ScheduleRound(5000, 0));
            }
        });
        return domain;
    }

    private SpeciallyNow buildDefaultSpeciallyNow() {
        SpeciallyNow domain = new SpeciallyNow();
        domain.setIs_send_msg(Cons.BOOLEAN_TRUE);
        domain.setMsg_id(DEFAULT_MSG_ID);
        domain.setIs_standby(Cons.BOOLEAN_TRUE);
        domain.setIs_online(Cons.BOOLEAN_TRUE);
        domain.setRounds(new ArrayList<ScheduleRound>() {
            private static final long serialVersionUID = 1L;
            {
                add(new ScheduleRound(5000, 0));
            }
        });
        return domain;
    }
}
